Ubuntu 16.04
Sponsored Link

OpenStack Queens : How to use Swift
2018/03/18
 
This section shows how to use Swift Storage from Clients.
This example is based on the emvironment like follows.
-+---------------+----------------------------+----------------------------+------------
 |               |                            |                            |
 |           eth0|10.0.0.30               eth0|10.0.0.50               eth0|10.0.0.61
 |   +-----------+-----------+    +-----------+-----------+    +-----------+-----------+
 |   |    [ Control Node ]   |    |    [  Proxy Node  ]   |    |    [    Client    ]   |
 |   |                       |    |                       |    |                       |
 |   |  MariaDB    RabbitMQ  |    |      Swift Proxy      |    |      Swift Client     |
 |   |  Memcached  httpd     |    |                       |    |                       |
 |   |  Keystone             |    |                       |    |                       |
 |   +-----------------------+    +-----------------------+    +-----------------------+
 |
 +---------------+----------------------------+----------------------------+-----------
             eth0|10.0.0.71               eth0|10.0.0.72               eth0|10.0.0.73
     +-----------+-----------+    +-----------+-----------+    +-----------+-----------+
     |   [ Storage Node#1 ]  |    |   [ Storage Node#2 ]  |    |   [ Storage Node#3 ]  |
     |                       |    |                       |    |                       |
     |     Swift-Account     |    |     Swift-Account     |    |     Swift-Account     |
     |    Swift-Container    |    |    Swift-Container    |    |    Swift-Container    |
     |     Swift-Object      |    |     Swift-Object      |    |     Swift-Object      |
     +-----------------------+    +-----------------------+    +-----------------------+

[1] Add a user for using Swift on Keystone Control Node.
By the way, if you'd like to use Swift Quickly, it's unnecessarry to add a new user, it's posibble to use with existing admin or swift user on Keystone.
# add swiftservice project

root@dlp ~(keystone)#
openstack project create --domain default --description "Swift Service Project" swiftservice

+-------------+----------------------------------+
| Field       | Value                            |
+-------------+----------------------------------+
| description | Swift Service Project            |
| domain_id   | default                          |
| enabled     | True                             |
| id          | 4510c6aa90f84b0d81b226b0701289f5 |
| is_domain   | False                            |
| name        | swiftservice                     |
| parent_id   | default                          |
| tags        | []                               |
+-------------+----------------------------------+

# add SwiftOperator role

root@dlp ~(keystone)#
openstack role create SwiftOperator

+-----------+----------------------------------+
| Field     | Value                            |
+-----------+----------------------------------+
| domain_id | None                             |
| id        | 88d18e60e4844d7fb3aad1814b9cb9e6 |
| name      | SwiftOperator                    |
+-----------+----------------------------------+

# add a user

root@dlp ~(keystone)#
openstack user create --domain default --project swiftservice --password userpassword user01

+---------------------+----------------------------------+
| Field               | Value                            |
+---------------------+----------------------------------+
| default_project_id  | 4510c6aa90f84b0d81b226b0701289f5 |
| domain_id           | default                          |
| enabled             | True                             |
| id                  | 78f834f43f5847ec8f619fc10a39618a |
| name                | user01                           |
| options             | {}                               |
| password_expires_at | None                             |
+---------------------+----------------------------------+

# add the user in SwiftOperator role

root@dlp ~(keystone)#
openstack role add --project swiftservice --user user01 SwiftOperator
[2] Work on a Client from here and later.
Install Keystoneclient, Swiftclient on a Client first.
root@client:~#
apt-get -y install python-openstackclient python-keystoneclient python-swiftclient
[3] Load environment variables first. ( it's just the value for a user added in [1] )
root@client:~#
vi ~/keystonerc_swift
export OS_PROJECT_DOMAIN_NAME=default
export OS_USER_DOMAIN_NAME=default
export OS_PROJECT_NAME=swiftservice
export OS_USERNAME=user01
export OS_PASSWORD=userpassword
export OS_AUTH_URL=http://10.0.0.30:5000/v3
export OS_IDENTITY_API_VERSION=3
export PS1='[\u@\h \W(swift)]\$ '
root@client:~#
chmod 600 ~/keystonerc_swift

root@client:~#
source ~/keystonerc_swift

root@client ~(swift)#
echo "source ~/keystonerc_swift " >> ~/.bash_profile
# show state

root@client ~(swift)#
swift stat

               Account: AUTH_4510c6aa90f84b0d81b226b0701289f5
            Containers: 0
               Objects: 0
                 Bytes: 0
       X-Put-Timestamp: 1521505770.14064
           X-Timestamp: 1521505770.14064
            X-Trans-Id: tx5ab3deee068b44098cfc7-005ab055e9
          Content-Type: text/plain; charset=utf-8
X-Openstack-Request-Id: tx5ab3deee068b44098cfc7-005ab055e9
[4] Create a Container for saving files.
root@client ~(swift)#
openstack container create test_container

+---------------------------------------+----------------+------------------------------------+
| account                               | container      | x-trans-id                         |
+---------------------------------------+----------------+------------------------------------+
| AUTH_4510c6aa90f84b0d81b226b0701289f5 | test_container | tx3baa2ab1ef554dd8b7f78-005ab05630 |
+---------------------------------------+----------------+------------------------------------+

root@client ~(swift)#
openstack container list

+----------------+
| Name           |
+----------------+
| test_container |
+----------------+
[5] Upload a file on local to the Container.
# upload test.txt

root@client ~(swift)#
openstack object create test_container test.txt

+----------+----------------+----------------------------------+
| object   | container      | etag                             |
+----------+----------------+----------------------------------+
| test.txt | test_container | a0d987ef6826c00ff6e4ac0851ea4744 |
+----------+----------------+----------------------------------+

root@client ~(swift)#
openstack object list test_container

+----------+
| Name     |
+----------+
| test.txt |
+----------+
[6] Download a file from Swift Storage to local.
# remove local file

root@client ~(swift)#
rm test.txt
root@client ~(swift)#
openstack object save test_container test.txt

root@client ~(swift)#
total 40
drwx------  3 root root 4096 Mar 20 09:31 ./
drwxr-xr-x 23 root root 4096 Mar  3 21:28 ../
-rw-------  1 root root 2657 Mar 15 19:27 .bash_history
-rw-r--r--  1 root root 3106 Oct 23  2015 .bashrc
drwx------  2 root root 4096 Mar 15 10:01 .cache/
-rw-------  1 root root  278 Mar 20 09:29 keystonerc_swift
-rw-r--r--  1 root root  148 Aug 18  2015 .profile
-rw-r--r--  1 root root   10 Mar 20 09:31 test.txt
-rw-------  1 root root 5767 Mar 20 09:29 .viminfo
[7] Delete a file on Swift Storage.
root@client ~(swift)#
openstack object delete test_container test.txt

root@client ~(swift)#
openstack object list test_container

# test.txt is deleted

[8] Delete a Container on Swift Storage.
root@client ~(swift)#
openstack container delete test_container

root@client ~(swift)#
openstack container list

# test_container is deleted
 
Tweet